package com.qiantu.phone.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.AttrRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;

import com.facebook.common.util.UriUtil;
import com.qiantu.phone.R;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/04/18
 *    desc   : 状态布局（网络错误，异常错误，空数据）
 */
public final class StatusLayout extends FrameLayout {

    /** 主布局 */
    private ViewGroup mMainLayout;
    /** 提示图标 */
    private ImageView ivStatusIcon;
//    private LottieAnimationView mLottieAnimationView;
    private SimpleAnimationDraweeView mLoadingView;
    private View mAnimationViewLayout;
    /** 提示文本 */
    private TextView mStatusTextView;
    /** 按钮 */
    private TextView mBtn;
    /** 重试监听 */
    private OnBtnClickListener mListener;
    private int mBackgroundResource;
    private Drawable mBackground;

    public StatusLayout(@NonNull Context context) {
        this(context, null);
    }

    public StatusLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StatusLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 显示
     */
    public void show() {

        if (mMainLayout == null) {
            //初始化布局
            initLayout();
        }

        if (isShow()) {
            return;
        }
        mBtn.setVisibility(mListener == null ? View.INVISIBLE : View.VISIBLE);
        // 显示布局
        mMainLayout.setVisibility(VISIBLE);
    }

    /**
     * 隐藏
     */
    public void hide() {
        if (mMainLayout == null || !isShow()) {
            return;
        }
        //隐藏布局
        mMainLayout.setVisibility(GONE);
    }

    /**
     * 是否显示了
     */
    public boolean isShow() {
        return mMainLayout != null && mMainLayout.getVisibility() == VISIBLE;
    }

    /**
     * 设置提示图标，请在show方法之后调用
     */
    public void setIconAndHint(@DrawableRes int icon, @StringRes int hint) {
        setIconAndHint(ContextCompat.getDrawable(getContext(), icon), getResources().getString(hint));
    }

//    public void setIconAndHint(Drawable drawable, CharSequence hint) {
//        if (mLottieAnimationView.isAnimating()) {
//            mLottieAnimationView.cancelAnimation();
//            mLottieAnimationView.setProgress(0);
//        }
//        mAnimationViewLayout.setVisibility(GONE);
//        mLottieAnimationView.setVisibility(GONE);
//        ivStatusIcon.setImageDrawable(drawable);
//        ivStatusIcon.setVisibility(VISIBLE);
//
//        if (TextUtils.isEmpty(hint)) {
//            mStatusTextView.setVisibility(GONE);
//        }else{
//            mStatusTextView.setText(hint);
//            mStatusTextView.setVisibility(VISIBLE);
//        }
//    }

    public void setIconAndHint(Drawable drawable, CharSequence hint) {
        mLoadingView.stopAnimation();
        mAnimationViewLayout.setVisibility(GONE);
        mLoadingView.setVisibility(GONE);
        ivStatusIcon.setImageDrawable(drawable);
        ivStatusIcon.setVisibility(VISIBLE);

        if (TextUtils.isEmpty(hint)) {
            mStatusTextView.setVisibility(GONE);
        }else{
            mStatusTextView.setText(hint);
            mStatusTextView.setVisibility(VISIBLE);
        }
    }

//    /**
//     * 设置提示动画
//     */
//    public void setAnimResource(@RawRes int id) {
//        mLottieAnimationView.setAnimation(id);
//        mLottieAnimationView.setRepeatCount(LottieDrawable.INFINITE);
//        if (!mLottieAnimationView.isAnimating()) {
//            mLottieAnimationView.playAnimation();
//        }
//        ivStatusIcon.setVisibility(GONE);
//        mStatusTextView.setVisibility(GONE);
//        mBtn.setVisibility(GONE);
//    }

    /**
     * 设置提示动画
     */
    public void setAnimResource(@DrawableRes int id) {
        mLoadingView.setImageURI(UriUtil.getUriForResourceId(id).toString());
        mLoadingView.startAnimation();
        ivStatusIcon.setVisibility(GONE);
        mStatusTextView.setVisibility(GONE);
        mBtn.setVisibility(GONE);
    }



    public void setBackgroundResource(@DrawableRes  int resId){
        mBackgroundResource = resId;
    }

    public void setBackground(Drawable drawable){
        mBackground = drawable;
    }

    /**
     * 初始化提示的布局
     */
    private void initLayout() {

        mMainLayout = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.widget_status_layout, this, false);

        ivStatusIcon = mMainLayout.findViewById(R.id.iv_status_icon);
//        mLottieAnimationView = mMainLayout.findViewById(R.id.lottie_animation_view);
        mLoadingView = mMainLayout.findViewById(R.id.loading_view);
        mAnimationViewLayout = mMainLayout.findViewById(R.id.animation_view_layout);
        mStatusTextView = mMainLayout.findViewById(R.id.tv_status_text);
        mBtn = mMainLayout.findViewById(R.id.btn);
        if(mBackgroundResource != 0){
            mMainLayout.setBackgroundResource(mBackgroundResource);
            mMainLayout.setClickable(true);
        }else if(mBackground != null){
            mMainLayout.setBackground(mBackground);
            mMainLayout.setClickable(true);
        }else{
            if (mMainLayout.getBackground() == null) {
                // 默认使用 windowBackground 作为背景
                TypedArray typedArray = getContext().obtainStyledAttributes(new int[]{android.R.attr.windowBackground});
                mMainLayout.setBackground(typedArray.getDrawable(0));
                mMainLayout.setClickable(true);
                typedArray.recycle();
            }
        }

        mBtn.setOnClickListener(mClickWrapper);

        addView(mMainLayout);
    }

    /**
     * 设置重试监听器
     */
    public void setOnBtnListener(CharSequence btnText, OnBtnClickListener listener) {
        mListener = listener;
        if (isShow()) {
            mBtn.setText(btnText);
            mBtn.setVisibility(mListener == null ? View.INVISIBLE : View.VISIBLE);
        }
    }

    /**
     * 点击事件包装类
     */
    private final OnClickListener mClickWrapper = new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mListener == null) {
                return;
            }
            mListener.onClick(StatusLayout.this);
        }
    };

    /**
     * 重试监听器
     */
    public interface OnBtnClickListener {

        /**
         * 点击了重试
         */
        void onClick(StatusLayout layout);
    }
}